package com.hs.auth.action;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.AUTHConstant;
import com.hs.auth.model.HsAccount;
import com.hs.auth.service.IUserService;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.MD5;
@Controller  
public class LogController1  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(LogController1.class);
	private IUserService userService;
	 @Autowired  
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
//	 /**
//	  * 用户登录
//	  * @param request
//	  * @param response
//	  * @return
//	  * @throws ServletException
//	  * @throws IOException
//	  */ 
//	@RequestMapping(value="/logon2.do")   
//	public ModelAndView logon(HttpServletRequest request,
//			HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException { 
//		StringBuffer buffer = new StringBuffer();
//		String viewname = "logon2";
//		String uid = request.getParameter("UserName");
//		String password = request.getParameter("Password");
//		String rememberme = request.getParameter("rememberme");
//		logger.debug("rememberme:" + rememberme);
//		if("1".equals(rememberme)){
//			rememberMe(uid,password,response);
//		}
//		//HsAccount obj = userService.getAccByUid(uid);
//		HsAccount obj = null;
//		Long objid = 0l;
//		DBMgr dbo = this.getDbo(request);
//		boolean accessflag = false;
//		boolean  ismimacuowu = false;
//		List dnmsuserlist = dbo.queryList("select * from HS_ACCOUNT1 where LOGINID=?", new Object[]{uid});
//		
//		if(null != dnmsuserlist&&dnmsuserlist.size()>0){
//			Map dnmsmap = (Map)dnmsuserlist.get(0);
//			String dbpassword = (String)dnmsmap.get("PASSWORD");
//			objid = ((java.math.BigDecimal)dnmsmap.get("ID")).longValue();
//			String inputpwd = MD5.md5(password);
//			System.out.println("inputpwd.equals(dbpassword):"+(inputpwd.equals(dbpassword)));
//			if(inputpwd.equals(dbpassword)){
//				accessflag = true;
//			}else{
//				buffer.append("密码输入错误!");
//				ismimacuowu = true;
//			}
//		}else{
//			 accessflag = false;
//			 buffer.append("【").append(uid).append("】").append("不存在!");
//		}
//		System.out.println("dnms:" + accessflag);
//		if(!accessflag){
//			
//			List dnmsbookuserlist = dbo.queryList("select * from HS_ACCOUNT where LOGINID=?", new Object[]{uid});
//			if(null != dnmsbookuserlist&&dnmsbookuserlist.size()>0){
//				buffer.delete(0, buffer.length()-1);
//				Map dnmsbookmap = (Map)dnmsbookuserlist.get(0);
//				String dbpassword = (String)dnmsbookmap.get("PASSWORD");
//				objid = ((java.math.BigDecimal)dnmsbookmap.get("ID")).longValue();
//				String inputpwd = MD5.md5(password);
//				if(inputpwd.equals(dbpassword)){
//					accessflag = true;
//				}else{
//					buffer.append("密码输入错误!");
//				}
//			}else{
//				 accessflag = false; 
//				 if(!ismimacuowu){
//					 buffer.append("【").append(uid).append("】").append("不存在!");
//				 }
//			}
//		}
//		System.out.println("dnmsab:" + accessflag);
//		
//		if(accessflag){
//			viewname = "muti/index";
//			obj = new HsAccount();
//			obj.setLoginid(uid);
//			obj.setId(objid);
//			this.initSession(request, obj);
//			this.loadAuth(request,obj.getId()+"");
//		} 
//		
//		
////		//根据用户名查询帐号信息
////		if(null != obj){
////			String dbpassword = obj.getPassword();
////			String inputpwd = MD5.md5(password);
////			if(inputpwd.equals(dbpassword)){
////				viewname = "muti/index";
////				this.initSession(request, obj);
////				this.loadAuth(request,obj.getId()+"");
////			} else{
////				buffer.append("密码输入错误!");
////			}
////		}else{
////			 buffer.append("【").append(uid).append("】").append("不存在!");
////		}   
//		
//		modelMap.put("alert", buffer.toString());
//		modelMap.put("username", uid);
//		return new ModelAndView(viewname,modelMap);  
//	}
	/**
	  * 用户登录
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */ 
	@RequestMapping(value="/logon2.do")   
	public ModelAndView logon(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException { 
		StringBuffer buffer = new StringBuffer();
		String viewname = "logon2";
		String uid = request.getParameter("UserName");
		String password = request.getParameter("Password");
		String rememberme = request.getParameter("rememberme");
		logger.debug("rememberme:" + rememberme);
		if("1".equals(rememberme)){
			rememberMe(uid,password,response);
		}
		//HsAccount obj = userService.getAccByUid(uid);
		HsAccount obj = null;
		Long objid = 0l;
		DBMgr dbo = this.getDbo(request);
		boolean accessflag = false;
		boolean  ismimacuowu = false;
		List dnmsuserlist = dbo.queryList("select * from HS_ACCOUNT1 where loginid=? union select * from HS_ACCOUNT where loginid=? ", new Object[]{uid,uid});
		
		if(null != dnmsuserlist&&dnmsuserlist.size()>0){
			for(int i =0;i<dnmsuserlist.size();i++){
				Map dnmsmap = (Map)dnmsuserlist.get(i);
				String dbpassword = (String)dnmsmap.get("PASSWORD");
				objid = ((java.math.BigDecimal)dnmsmap.get("ID")).longValue();
				String inputpwd = MD5.md5(password);
				System.out.println("map:"+dnmsmap);
				System.out.println("equals(dbpassword):"+(inputpwd.equals(dbpassword)));
				System.out.println("dbpassword:"+dbpassword);
				System.out.println("inputpwd:"+inputpwd);
				if(inputpwd.equals(dbpassword)){
					accessflag = true;
					break;
				}
			}
			if(!accessflag){ 
				buffer.append("密码输入错误!");
				ismimacuowu = true; 
			}
		}else{
			 accessflag = false;
			 buffer.append("【").append(uid).append("】").append("不存在!");
		}
		
		
		if(accessflag){
			viewname = "muti/index";
			obj = new HsAccount();
			obj.setLoginid(uid);
			obj.setId(objid);
			this.initSession(request, obj);
			//this.loadAuth(request,obj.getId()+"");
		} 
		
		
//		//根据用户名查询帐号信息
//		if(null != obj){
//			String dbpassword = obj.getPassword();
//			String inputpwd = MD5.md5(password);
//			if(inputpwd.equals(dbpassword)){
//				viewname = "muti/index";
//				this.initSession(request, obj);
//				this.loadAuth(request,obj.getId()+"");
//			} else{
//				buffer.append("密码输入错误!");
//			}
//		}else{
//			 buffer.append("【").append(uid).append("】").append("不存在!");
//		}   
		
		modelMap.put("alert", buffer.toString());
		modelMap.put("username", uid);
		return new ModelAndView(viewname,modelMap);  
	}
	
	
	private void rememberMe(String uid,String password,HttpServletResponse response){
		Cookie c = new Cookie("dnmsabname2",uid); 
 		Cookie pwd = new Cookie("dnmsabpwd2",password); 

 		c.setMaxAge(30*24*60*60); //设置过期之前的最长时间    
  		//设置Cookie路径和域名 
 		//c.setDomain("cintcm.ac.cn") ;
 		response.addCookie(c); 
 		c.setMaxAge(30*24*60*60); //设置过期之前的最长时间    
  		//设置Cookie路径和域名 
 		//c.setDomain("cintcm.ac.cn") ; 
 		response.addCookie(pwd);
 		pwd.setMaxAge(30*24*60*60);
	}
	 /**
	  * 用户登出
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/logout2.do")   
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
		StringBuffer buffer = new StringBuffer(); 
		logger.info("In the logController logout method!");
		HttpSession session = request.getSession();
		String userId = "";
		String userName = "";
		HsAccount accinfo = (HsAccount)session.getAttribute(AUTHConstant.USERINSESSION);
		if(null != accinfo){
			userId = accinfo.getId()+"";
 		}
		session.setAttribute(AUTHConstant.USERINSESSION, null); 
		session.setAttribute("USERPOWER_" + userId, null);
		//LogUtil.log(req,userName+"-退出系统",LogUtil.LOG_OUT,userId);
		if(null != session)
			session.invalidate(); 
		return new ModelAndView("/logon2");  
	}
	 
	 
	 
}
